using UnityEngine;

namespace Gj
{
    public class LocationExtend : MonoBehaviour
    {
        public RectTransform Rect;
        public bool left;
        public bool top;
        public bool right;
        public bool bottom;

        public Vector3 offset;

        public void Magnet()
        {
            if (left)
            {
                transform.position = new Vector3(Rect.transform.position.x - Rect.rect.width * Rect.pivot.x,
                    transform.position.y, 0) + offset;
                return;
            }

            if (right)
            {
                transform.position = new Vector3(Rect.transform.position.x + Rect.rect.width * (1 - Rect.pivot.x),
                    transform.position.y, 0) + offset;
                return;
            }

            if (top)
            {
                transform.position = new Vector3(transform.position.x,
                    Rect.transform.position.y + Rect.rect.height * (1 - Rect.pivot.y), 0) + offset;
                return;
            }

            if (bottom)
            {
                Debug.LogErrorFormat("{0} {1} {2} {3}", Rect.transform.position.y, Rect.rect.height, Rect.pivot.y,
                    offset);
                transform.position = new Vector3(transform.position.x,
                    Rect.transform.position.y - Rect.rect.height * Rect.pivot.y, 0) + offset;
            }
        }
    }
}